<?php


namespace app\admin\model;


use think\Db;

class OrderModel extends BaseModel
{
    protected $name='order';
    protected   $cate_arr=[
        1=>'商品墓',
        2=>'艺术墓',
        3=>'生态墓',
        4=>'节地墓'
    ];
    protected   $regulation_arr=[
        0=>'',
        1=>'单穴',
        2=>'双穴',
        3=>'三穴',
    ];
    protected   $col_status=[
        0=>'无产品',
        1=>'空闲',
        2=>'已认购',
        3=>'定金',
        4=>'无规划',
        5=>'购买未使用',
        6=>'部分安葬',
        7=>'全部安葬',
        8=>'迁出',
    ];

    /**
     * @param int $id
     * @param int $type 0 购买人id 1 订单id
     * @return array|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public  function getOrderByCustomerId($id,$type=0){
        if ($type!=0){
            $where = "o.is_delete = 0 and o.order_id = '{$id}' and (o.status = 0 or o.status = 1 or o.status = 2)";
        }else{
            $where = "o.is_delete = 0 and  o.customer_id = '{$id}' and (o.status = 0 or o.status = 1 or o.status = 2)";
        }
        $field='p.*,o.*,ci.buyer_name,ci.customer_account,ci.buyer_phone,ci.cart_type,ci.customer_address,c.col_status,c.reacher,c.col_status,c.reacher,c.column_num,c.colID,ci.buyer_sex,ci.cart_upload_id,ci.cart_upload';
        $result =  Db::name('order')->alias('o')->field($field)
            ->join("dj_tombarea_row_col c","o.tomb_id = c.colID")
            ->join("dj_customer_info ci","o.customer_id = ci.id")
            ->join("dj_tombarea_information_product p","o.tomb_product_id = p.id")
            ->where($where)->find();

        if ($result) {

            $bxq_end_date = $result['bxq_end_date'];//保修
            $dz_create_time = $result['dz_create_time'];//打证日期
            $whq_start_date = $result['whq_start_date'];//维护期起
            $whq_end_date = $result['whq_end_date'];//维护期截止
            if ($result['tomb_typei'] == 1) {
                $deposit_end_time = unixtime_to_date_inc($result['add_time'], intval($result['deposit_storage']));
                if ($bxq_end_date > time()) {
                    $result['deposit_end_time'] = unixtime_to_date($deposit_end_time, 'Y年m月d日') . '到期';
                } else {
                    $int = time() - $deposit_end_time;
                    $str = sprintf('%d天', floor($int / 86400));
                    $result['deposit_end_time'] = '已超' . $str;;
                }
            } else {
            }
            $result['gm_start_time'] = unixtime_to_date($result['add_time'], 'Y年m月d日'); //购买日期
            $result['dz_start_time'] = unixtime_to_date($dz_create_time, 'Y年m月d日'); //打证时间
            if ($bxq_end_date) {
                if ($bxq_end_date > time()) {
                    $result['end_bxq_time'] = unixtime_to_date($bxq_end_date, 'Y年m月d日') . '到期';//保修期止 warranty
                    $result['bxq_status'] = '未到期';//状态
                    $result['is_bxq_status'] = 1;//状态
                } else {
                    $result['end_bxq_time'] = unixtime_to_date($bxq_end_date, 'Y年m月d日');//保修期止 warranty
                    $result['bxq_status'] = '已到期';//状态
                    $result['is_bxq_status'] = -1;//状态
                }
            } else {
                $result['end_bxq_time'] = '';//保修期止 warranty
                $result['bxq_status'] = '已到期';//状态
                $result['is_bxq_status'] = -1;//状态
            }

            if ($whq_end_date) {
                $result['start_whq_date'] = unixtime_to_date($whq_start_date, 'Y年m月d日'); //维护期起
                if ($whq_end_date > time()) {
                    $result['end_whq_time'] = unixtime_to_date($whq_end_date, 'Y年m月d日') . '到期';//strtotime( $ad_info['date_funeral']))维护期止  maintain
                    $result['whq_status'] = '未到期';//状态  maintain
                    $result['is_whq_status'] = 1;//状态
                } else {
                    $int = time() - $whq_end_date;
                    $str = sprintf('%d天', floor($int / 86400));
                    $result['end_whq_time'] = '已超' . $str;//维护期止  maintain
                    $result['whq_status'] = '已到期';//状态
                    $result['is_whq_status'] = -1;//状态
                }
            } else {
                $result['start_whq_date'] = '';//状态  maintain
                $result['end_whq_time'] = '';//状态  maintain
                $result['whq_status'] = '未起计';//状态  maintain
                $result['is_whq_status'] = 0;//状态
            }

            if ($result['tomb_type']==1) {
                if ($result['discount_ratio'] == 0) {
                    $result['discount_ratio'] = '无优惠';
                } else {
                    $result['discount_ratio'] = 100 - ($result['discount_ratio'] * 100) . '%';
                }
            }elseif ($result['tomb_type']==2){
            }elseif ($result['tomb_type']==3){
                $result['discount_ratio']= $result['discount_ratio'] .'元';
            }elseif ($result['tomb_type']==4){
                if ($result['tomb_typei']==0){

                }else{
                    $result['discount_price'] = '减免'.$result['discount_price'].'年';
                }
            }else{
                $result['discount_ratio']= $result['discount_ratio'] .'元';
            }

            $result['col_status_name'] = $this->col_status[$result['col_status']];
            $result['tomb_regulation_name'] = $this->regulation_arr[$result['tomb_regulation']];
            $result['shoppingguide'] = cmf_get_admin_name($result['shoppingguide']);
            $result['agent_user'] = cmf_get_admin_name($result['agent_user']);

            return $result;
        }else{
            return [];
        }
    }

    /**
     *
     * @param int $id
     * @param int $type 0 购买人id 1 订单id
     * @return array|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public  function getRefundOrderBy($id,$type=0){
        if ($type!=0){
            $where = "o.order_id = '{$id}'  ";
        }else{
            $where = "o.customer_id = '{$id}' ";
        }
        $field='p.*,o.*,ci.buyer_name,ci.customer_account,ci.buyer_phone,ci.cart_type,ci.customer_address,c.col_status,c.reacher,c.col_status,c.reacher,c.column_num,c.colID,ci.buyer_sex,ci.cart_upload_id,ci.cart_upload';
        $result =  Db::name('order')->alias('o')->field($field)
            ->join("dj_tombarea_row_col c","o.tomb_id = c.colID")
            ->join("dj_customer_info ci","o.customer_id = ci.id")
            ->join("dj_tombarea_information_product p","o.tomb_product_id = p.id")
            ->where($where)->find();
        if ($result) {


            $bxq_end_date = $result['bxq_end_date'];//保修
            $dz_create_time = $result['dz_create_time'];//打证日期
            $whq_start_date = $result['whq_start_date'];//维护期起
            $whq_end_date = $result['whq_end_date'];//维护期截止
            if ($result['tomb_typei'] == 1) {
                $deposit_end_time = unixtime_to_date_inc($result['add_time'], intval($result['deposit_storage']));
                if ($bxq_end_date > time()) {
                    $result['deposit_end_time'] = unixtime_to_date($deposit_end_time, 'Y年m月d日') . '到期';
                } else {

                    $int = time() - $deposit_end_time;
                    $str = sprintf('%d天', floor($int / 86400));
                    $result['deposit_end_time'] = '已超' . $str;;
                }
            } else {
            }
            if (empty($whq_end_date)) {
                $ad_info = ArchivesDeceasedModel::where(['tomb_id' => $result['tomb_id'],'customer_id' => $result['customer_id']])->order('deceased_id asc ')->field('date_funeral,funeral_time')->find();
                if ($ad_info) {
                    OrderModel::update(['whq_start_date' => date_to_unixtime($ad_info['funeral_time']), 'whq_end_date' => unixtime_to_date_inc(date_to_unixtime($ad_info['funeral_time']), 3)], ['order_id' => $result['order_id']]);
                    $whq_start_date = date_to_unixtime($ad_info['funeral_time']);
                    $whq_end_date = unixtime_to_date_inc(date_to_unixtime($ad_info['funeral_time']), 3);
                }
            }
            $result['gm_start_time'] = unixtime_to_date($result['add_time'], 'Y年m月d日'); //购买日期
            $result['dz_start_time'] = unixtime_to_date($dz_create_time, 'Y年m月d日'); //打证时间
            if ($bxq_end_date) {
                if ($bxq_end_date > time()) {
                    $result['end_bxq_time'] = unixtime_to_date($bxq_end_date, 'Y年m月d日') . '到期';//保修期止 warranty
                    $result['bxq_status'] = '未到期';//状态
                } else {
                    $result['end_bxq_time'] = unixtime_to_date($bxq_end_date, 'Y年m月d日');//保修期止 warranty
                    $result['bxq_status'] = '已到期';//状态
                }
            } else {
                $result['end_bxq_time'] = '';//保修期止 warranty
                $result['bxq_status'] = '已到期';//状态
            }


            if ($whq_end_date) {
                $result['start_whq_date'] = unixtime_to_date($whq_start_date, 'Y年m月d日'); //维护期起
                if ($whq_end_date > time()) {
                    $result['end_whq_time'] = unixtime_to_date($bxq_end_date, 'Y年m月d日') . '到期';//strtotime( $ad_info['date_funeral']))维护期止  maintain
                    $result['whq_status'] = '未到期';//状态  maintain
                } else {
                    $int = time() - $whq_end_date;
                    $str = sprintf('%d天', floor($int / 86400));
                    $result['end_whq_time'] = '已超' . $str;//维护期止  maintain
                    $result['whq_status'] = '已到期';//状态
                }
            } else {
                $result['start_whq_date'] = '';//状态  maintain
                $result['end_whq_time'] = '';//状态  maintain
                $result['whq_status'] = '未起计';//状态  maintain
            }

            $result['col_status_name'] = $this->col_status[$result['col_status']];
            $result['tomb_regulation_name'] = $this->regulation_arr[$result['tomb_regulation']];
            $result['shoppingguide'] = cmf_get_admin_name($result['shoppingguide']);
            $result['agent_user'] = cmf_get_admin_name($result['agent_user']);

            return $result;
        }else{
            return [];
        }
    }


    public static function getOrderByid($order_id){
        $field='o.order_id,o.sign_time,o.dz_time,o.sj_time,o.whq_start_date,o.whq_end_date,p.warranty,p.maintain,ci.is_dy_mz';
        $result =  Db::name('order')->alias('o')->field($field)
            ->join("dj_customer_info ci","o.customer_id = ci.id")
            ->join("dj_tombarea_row_col c","o.tomb_id = c.colID")
            ->join("dj_tombarea_information_product p","o.tomb_product_id = p.id")
            ->where( "o.order_id = '{$order_id}'")->find();

        return $result;
    }


}